﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RecipientIdentity
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Security.Cryptography;
using System.ServiceModel;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class RecipientIdentity
  {
    private string m_identifier = string.Empty;
    private string m_organizationIdentifier = string.Empty;
    private string m_organizationPPIDIdentifier = string.Empty;
    private EndpointAddress m_address;
    private bool m_hasBeenValidated;

    public static RecipientIdentity CreateIdentity(
      EndpointAddress epr,
      bool validate)
    {
      if (epr.Identity is X509CertificateEndpointIdentity)
      {
        if (((X509CertificateEndpointIdentity) epr.Identity).Certificates == null || ((X509CertificateEndpointIdentity) epr.Identity).Certificates.Count < 1)
          throw InfoCardTrace.ThrowHelperError((Exception) new PolicyValidationException());
        RecipientIdentity recipientIdentity = (RecipientIdentity) new X509RecipientIdentity(epr, ((X509CertificateEndpointIdentity) epr.Identity).Certificates);
        if (validate)
          recipientIdentity.Validate();
        return recipientIdentity;
      }
      if (epr.Identity != null)
        throw InfoCardTrace.ThrowHelperError((Exception) new IdentityValidationException(SR.GetString("UnsupportedIdentityType")));
      RecipientIdentity recipientIdentity1 = new RecipientIdentity(epr);
      if (validate)
        recipientIdentity1.Validate();
      return recipientIdentity1;
    }

    public RecipientIdentity(EndpointAddress epr)
    {
      this.m_address = epr;
    }

    public EndpointAddress Address
    {
      get
      {
        return this.m_address;
      }
    }

    protected bool HasBeenValidated
    {
      set
      {
        this.m_hasBeenValidated = value;
      }
      get
      {
        return this.m_hasBeenValidated;
      }
    }

    protected string Identifier
    {
      set
      {
        this.m_identifier = value;
      }
    }

    protected string OrganizationIdentifier
    {
      set
      {
        this.m_organizationIdentifier = value;
      }
    }

    protected string OrganizationPPIDIdentifier
    {
      set
      {
        this.m_organizationPPIDIdentifier = value;
      }
    }

    public virtual void Validate()
    {
      if (this.m_hasBeenValidated)
        return;
      using (SHA256 shA256 = (SHA256) new SHA256Managed())
      {
        this.m_identifier = Convert.ToBase64String(shA256.ComputeHash(Encoding.Unicode.GetBytes(this.m_address.Uri.Host)));
        this.m_organizationIdentifier = this.m_identifier;
        this.m_organizationPPIDIdentifier = this.m_identifier;
      }
      this.m_hasBeenValidated = true;
    }

    public virtual string GetIdentifier()
    {
      InfoCardTrace.Assert(this.HasBeenValidated, "Identity has not been validated");
      return this.m_identifier;
    }

    public virtual string GetOrganizationIdentifier()
    {
      InfoCardTrace.Assert(this.HasBeenValidated, "Identity has not been validated");
      return this.m_organizationIdentifier;
    }

    public virtual string GetOrganizationPPIDIdentifier()
    {
      InfoCardTrace.Assert(this.HasBeenValidated, "Identity has not been validated");
      return this.m_organizationPPIDIdentifier;
    }

    public virtual string GetName()
    {
      return this.m_address.Uri.Host;
    }
  }
}
